#include "BeginRecvReqHandler.h"
#include "EndRecvReqHandler.h"

using namespace log4cplus;

Logger BeginRecvReqHandler::log=GetLogger(LOG4CPLUS_TEXT("BeginRecvReqHandler"));

ITransHandler::ExecuteResultCode BeginRecvReqHandler::Execute(ClientContext &client,OVERLAPPED *ovl)
{
	unsigned int clienSequenceNo=client.SequenceNo();
	ExecuteResultCode ret=ITransHandler::ExecuteOK;
	ITransHandler *nextHandler = new EndRecvReqHandler();
	ITransHandler *self = client.SetCurrentHandler(nextHandler);

	LOG4CPLUS_DEBUG_FMT(log,LOG4CPLUS_TEXT("[Client%d]Start to receive first client request."),clienSequenceNo);
	DWORD flags=0;
	int result = ::WSARecv(client.SrcSock(),client.Buffer(),1,NULL,&flags,client.OVL(),NULL);
	int errCode=::WSAGetLastError();
	if(result!=0&&errCode!=WSA_IO_PENDING)
	{
		ret=BeginRecvReqHandler::ExecuteFatalError;
		LOG4CPLUS_ERROR_FMT(log,LOG4CPLUS_TEXT("[Client%d]Begin receive data failed,error code:%d."),clienSequenceNo,errCode);
	}
	delete self;
	return ret;
}